package ua.kharkov.khpi.tarasenko.bach.analysis.tnr;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class PseudoDbTNR {
	// the largest item in the database
	public int maxItem = 0;
	// the number of transactions in the database
	public int tidsCount = 0;

	// The list of transactions in this database
	private final List<Transaction> transactions = new ArrayList<Transaction>();

	/**
	 * Read a transaction from the file and store it into memory
	 * 
	 * @param itemsString
	 *            a set of items as String.
	 */
	public void addTransaction(String itemsString[]) {
		// we create an object Transaction to store the items
		Transaction transaction = new Transaction(itemsString.length);

		// for each item (String)
		for (String itemString : itemsString) {
			// if it is empty, skip it
			if ("".equals(itemString)) {
				continue;
			}
			// convert from string to integer
			int item = Integer.parseInt(itemString);
			// if the item is larger than the largest item, remember that
			if (item >= maxItem) {
				maxItem = item;
			}
			// add the item to the transaction
			transaction.addItem(item);
		}
		// increase the number of transactions
		tidsCount++;

		// add the transaction to the transaction database
		transactions.add(transaction);

		// Sort transactions by descending order of items because
		// TopKRules and TNR assume that items are sorted by lexical order
		// for optimization.
		Collections.sort(transaction.getItems(), new Comparator<Integer>() {
			public int compare(Integer o1, Integer o2) {
				return o2 - o1;
			}
		});
	}

	/**
	 * Get the number of transactions.
	 * 
	 * @return the number of transactions
	 */
	public int size() {
		return transactions.size();
	}

	/**
	 * Get the list of transactions in this database.
	 * 
	 * @return a List of Transactions
	 */
	public List<Transaction> getTransactions() {
		return transactions;
	}
}
